/*
 * Diagrama.java
 *
 * Created on June 6, 2007, 10:29 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface.JGraph_Algebra;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.ScrollPaneLayout;


import org.jerm.Interface.JGraph_Algebra.*;
import org.jerm.Mapeamento.Atributos.*;
import org.jerm.Mapeamento.Tabelas.*;
import org.jgraph.JGraph;
import org.jgraph.event.GraphSelectionEvent;
import org.jgraph.event.GraphSelectionListener;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;



/**
 *
 * @author tiagofalcao
 */
public class Diagrama_JGraph extends JScrollPane implements GraphSelectionListener, KeyListener
{
    //JGraph
    protected JGraph graph;
    
    //Arvore de consulta
    private Consulta consulta;
    
    private Vector consultas;
    
    private Diagrama_ToolBar toolbar=null;
            
    /** Creates a new instance of Diagrama */
    public Diagrama_JGraph() {
        this.consulta = null;
        
        setLayout(new ScrollPaneLayout());
        
        this.MontaDiagrama();
        this.MontaTextarea();
    }
        
    private void MontaDiagrama()
    {
        graph = new MyGraph(new DefaultGraphModel());
        graph.getSelectionModel().addGraphSelectionListener(this);
        graph.addKeyListener(this);
        this.setViewportView(graph);
    }
    
    private void MontaTextarea()
    {
        //AlgebraToSQL sql = new AlgebraToSQL(this.consulta);
        //System.out.println(sql.getSQL());
    }
    
    public void insere(Consulta consulta)
    {
        graph.getGraphLayoutCache().insert(consulta.getCells());
    }
    
    public JGraph getGraph()
    {
        return this.graph;
    }
    
    
    /////////////////////////////////////////////////////////////////////////
    //
    // KeyListener for Delete KeyStroke
    //
    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
            // Listen for Delete Key Press
            if (e.getKeyCode() == KeyEvent.VK_DELETE){// Execute Remove Action on Delete Key Press
                    Object[] cells = graph.getSelectionCells();
                    cells = graph.getDescendants(cells);
                    graph.getModel().remove(cells);
                    graph.getGraphLayoutCache().reload();
            }
//            else if(e.getModifiersEx()== KeyEvent.CTRL_DOWN_MASK && e.getKeyCode() == KeyEvent.VK_Z)
//                undo.actionPerformed(null);
//            else if(e.getModifiersEx()== KeyEvent.CTRL_DOWN_MASK && e.getKeyCode() == KeyEvent.VK_R)
//                redo.actionPerformed(null);
//            else if(e.getModifiersEx()== KeyEvent.CTRL_DOWN_MASK && e.getKeyCode() == KeyEvent.VK_X)
//                cut.actionPerformed(null);
//            else if(e.getModifiersEx()== KeyEvent.CTRL_DOWN_MASK && e.getKeyCode() == KeyEvent.VK_C)
//                copy.actionPerformed(null);
//            else if(e.getModifiersEx()== KeyEvent.CTRL_DOWN_MASK && e.getKeyCode() == KeyEvent.VK_V)
//                paste.actionPerformed(null);
            
    }

    public void setToolBar(Diagrama_ToolBar t){
        this.toolbar=t;
    }
    
    public void valueChanged(GraphSelectionEvent e) {
        if(toolbar!=null){
            Object[] s=graph.getSelectionCells();
            int q=0;
            int i=0;
            while(i<s.length){
                if(s[i].getClass()==Produto.class || s[i].getClass()==Juncao.class ||
                        s[i].getClass()==Selecao.class || s[i].getClass()==Projecao.class ||
                        s[i].getClass()==Relacao.class || s[i].getClass()==Renomeacao.class)
                    q++;
                i++;
            }
            if(q==1){
                toolbar.pi.setEnabled(true);
                toolbar.sigma.setEnabled(true);
                toolbar.ro.setEnabled(true);
                toolbar.produto.setEnabled(false);
                toolbar.join.setEnabled(false);
            }else if(q==2){
                toolbar.pi.setEnabled(false);
                toolbar.sigma.setEnabled(false);
                toolbar.ro.setEnabled(false);
                toolbar.produto.setEnabled(true);
                toolbar.join.setEnabled(true);
            }else{
                toolbar.pi.setEnabled(false);
                toolbar.sigma.setEnabled(false);
                toolbar.ro.setEnabled(false);
                toolbar.produto.setEnabled(false);
                toolbar.join.setEnabled(false);
            }
        }
    }
    
}
